//
//  RHMeAddUserCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/17.
//

#import "RHMeAddUserCell.h"
#import "RHAddCommonModel.h"

@interface RHMeAddUserCell()<QMUITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUITextField *textField;

@property (nonatomic, strong) QMUIButton *btn;


@end
@implementation RHMeAddUserCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHAddCommonModel *)model
{
    _model = model;
    if (model.isRequired) {
        NSString *titleStr = [@"*" stringByAppendingString:model.title];
        self.titleLab.attributedText = [NSString attributeStringWithContent:titleStr attriFont:kSYSTEMFONT(14) attriColor:kCOLOR_HEX(@"#F95454") attriKey:@"*"];
    }else{
        self.titleLab.text = model.title;
    }
    self.textField.placeholder = model.placeHolder;
    self.textField.text = model.content;
    if (model.enum_content) {
        self.textField.text = model.enum_content;
    }
    self.arrowImg.hidden = !model.isSelection;
    self.btn.hidden = !model.isSelection;
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(self.model.deviceList.count*44);
    }];
    [self.tableView reloadData];
}


-(void)setTopBorder
{
    self.bgView.layer.cornerRadius = 8;
    self.bgView.qmui_borderColor = kBorderColor;
    self.bgView.qmui_borderWidth = 1;
    self.bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionTop|QMUIViewBorderPositionRight;
    self.bgView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner|QMUILayerMaxXMinYCorner;
    self.bgView.layer.masksToBounds = YES;
}
-(void)setMiddleBorder
{
    self.bgView.layer.cornerRadius = 0;
    self.bgView.qmui_borderColor = kBorderColor;
    self.bgView.qmui_borderWidth = 1;
    self.bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight;
    self.bgView.layer.masksToBounds = YES;
}
-(void)setBottomBorder
{
    self.bgView.layer.cornerRadius = 8;
    self.bgView.qmui_borderColor = kBorderColor;
    self.bgView.qmui_borderWidth = 1;
    self.bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionBottom|QMUIViewBorderPositionRight;
    self.bgView.layer.qmui_maskedCorners = QMUILayerMinXMaxYCorner|QMUILayerMaxXMaxYCorner;
    self.bgView.layer.masksToBounds = YES;
}

- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(50);
        make.top.bottom.mas_offset(0);
    }];
    
    _titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    _titleLab.numberOfLines = 0;
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.mas_equalTo(kScaleW(100));
    }];
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:arrowImg];
    self.arrowImg = arrowImg;
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
    
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        make.right.equalTo(arrowImg.mas_left).offset(-5);
        make.height.mas_equalTo(30);
        make.centerY.mas_offset(0);
    }];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];

    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    
    
    _btn = [[QMUIButton alloc]init];
    [bgView addSubview:_btn];
    [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 15, 10, 15));
    }];
    
    [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.clickBlock){
            weakSelf.clickBlock();
        }
    }];
    
  
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([self.model.tag isEqualToString:@"user_organization"]) {
        return;
    }
    self.model.content = textField.text;
}

@end
